import {
  saveOrUpdateCoupon ,listPluginCoupon,delPluginCoupon,checkUserList,getPluginCouponById ,loadCouponResultChart,listPluginCouponResult
} from '../services/coupon';
import { routerRedux } from 'dva/router';

export default {
  namespace: 'coupon',

  state: {
    index:0,
    loading: true,
    data: [],
    couponListData: [],
    checkUserListData:[],
  },
  effects: {
    *fetchListPluginCoupon({payload,callback}, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(listPluginCoupon,payload);
      yield put({
        type: 'listPluginCoupon',
        payload: response.data
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response.data)
    }
    ,
    *fetchDelCoupon({payload,callback}, { call, put }) {
      const response = yield call(delPluginCoupon,payload);
      callback(response)
      yield put({
        type: 'listPluginCoupon',
        payload: response
      });
    }
    ,
    *couponAddForm({payload}, { put }) {
      if (payload) {
        yield put(routerRedux.push({
          pathname:'/rdActivity/CouponAdd',
          query:{
            ...payload
          }
        }));
      }else{
        yield put(routerRedux.push(
          '/rdActivity/CouponAdd'
        ));
      }
    }
    ,
    *couponDetails({payload}, { put }) {
        yield put(routerRedux.push({
          pathname:'/rdActivity/CouponDetails',
          query:{
            pcId:payload.pcId
          }
        }));
    }
    ,
    *fetchGetPluginCouponById({payload,callback}, { call,put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getPluginCouponById,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response.data)
    }
    ,
    *fetchSaveOrUpdateCoupon({payload ,callback}, { call,put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(saveOrUpdateCoupon,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response)
    }
    ,
    *fetchCheckUserList({payload ,callback}, { call,put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(checkUserList,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response.data)
    }
    ,
    *fetchLoadCouponResultChart({payload ,callback}, { call,put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(loadCouponResultChart,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response.data)
    }
    ,
    *fetchListPluginCouponResult({payload ,callback}, { call,put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(listPluginCouponResult,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response.data)
    }
  },

  reducers: {
    listPluginCoupon(state, action) {
      return {
        ...state,
        couponListData: action.payload
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
  }
};
